Population class extension: sampling variables functions module
Main script to provide the sampling variable class extension. Here the sampling variable codebase is defined.
- class binarycpython.utils.population_extensions.sampling_variables.sampling_variables(**kwargs)[source]
- Bases: - object- Extension for the Population class containing the code for the sampling variable. - add_grid_variable(name, parameter_name, longname, valuerange, samplerfunc, probdist, dphasevol=- 1, gridtype='centred', branchpoint=0, branchcode=None, precode=None, postcode=None, topcode=None, bottomcode=None, condition=None, dry_parallel=False)[source]
- NOTE: this function exists for backward compatability with older scripts. Please use add_sampling_variable from now on. - Function to add sampling variables to the population_options. Calls add_sampling_variable(). - The execution of the sampling generation will be through a nested for loop. Each of the grid variables will get create a deeper for loop. - The real function that generates the numbers will get written to a new file in the TMP_DIR, and then loaded imported and evaluated. beware that if you insert some destructive piece of code, it will be executed anyway. Use at own risk. - Parameters
- name ( - str) –- name of parameter used in the grid Python code. This is evaluated as a parameter and you can use it throughout the rest of the function - Examples: - name = 'lnM_1' 
- parameter_name ( - str) –- name of the parameter in binary_c - This name must correspond to a Python variable of the same name, which is automatic if parameter_name == name. - Note: if parameter_name != name, you must set a
- variable in “precode” or “postcode” to define a Python variable called parameter_name 
 
- longname ( - str) –- Long name of parameter - Examples: - longname = 'Primary mass' 
- range – - Range of values to take. Does not get used really, the samplerfunc is used to get the values from - Examples: - range = [math.log(m_min), math.log(m_max)] 
- samplerfunc ( - str) –- Function returning a list or numpy array of samples spaced appropriately. You can either use a real function, or a string representation of a function call. - Examples: - samplerfunc = "self.const_linear(math.log(m_min), math.log(m_max), {})".format(resolution['M_1']) 
- precode ( - Optional[- str]) –- Extra room for some code. This code will be evaluated within the loop of the sampling function (i.e. a value for lnM_1 is chosen already) - Examples: - precode = 'M_1=math.exp(lnM_1);' 
- postcode ( - Optional[- str]) – Code executed after the probability is calculated.
- probdist ( - str) –- Function determining the probability that gets assigned to the sampled parameter - Examples: - probdist = 'self.Kroupa2001(M_1)*M_1' 
- dphasevol ( - Union[- str,- int]) –- part of the parameter space that the total probability is calculated with. Put to -1 if you want to ignore any dphasevol calculations and set the value to 1 - Examples: - dphasevol = 'dlnM_1' 
- condition ( - Optional[- str]) –- condition that has to be met in order for the grid generation to continue - Examples: - condition = "self.population_options['binary']==1" 
- gridtype ( - str) – Method on how the value range is sampled. Can be either ‘edge’ (steps starting at the lower edge of the value range) or ‘centred’ (steps starting at- lower edge + 0.5 * stepsize).
- dry_parallel ( - Optional[- bool]) – If True, try to parallelize this variable in dry runs.
- topcode ( - Optional[- str]) – Code added at the very top of the block.
- bottomcode ( - Optional[- str]) – Code added at the very bottom of the block.
 
- Return type
- None
 
 - add_sampling_variable(name, parameter_name, longname, valuerange, samplerfunc, probdist, dphasevol=- 1, gridtype='centred', branchpoint=0, branchcode=None, precode=None, postcode=None, topcode=None, bottomcode=None, condition=None, dry_parallel=False, dependency_variables=None)[source]
- Function to add sampling variables to the population_options. - The execution of the sampling generation will be through a nested for loop. Each of the grid variables will get create a deeper for loop. - The real function that generates the numbers will get written to a new file in the TMP_DIR, and then loaded imported and evaluated. beware that if you insert some destructive piece of code, it will be executed anyway. Use at own risk. - Parameters
- name ( - str) –- name of parameter used in the grid Python code. This is evaluated as a parameter and you can use it throughout the rest of the function - Examples: - name = 'lnM_1' 
- parameter_name ( - str) –- name of the parameter in binary_c - This name must correspond to a Python variable of the same name, which is automatic if parameter_name == name. - Note: if parameter_name != name, you must set a
- variable in “precode” or “postcode” to define a Python variable called parameter_name 
 
- longname ( - str) –- Long name of parameter - Examples: - longname = 'Primary mass' 
- range – - Range of values to take. Does not get used really, the samplerfunc is used to get the values from - Examples: - range = [math.log(m_min), math.log(m_max)] 
- samplerfunc ( - str) –- Function returning a list or numpy array of samples spaced appropriately. You can either use a real function, or a string representation of a function call. - Examples: - samplerfunc = "self.const_linear(math.log(m_min), math.log(m_max), {})".format(resolution['M_1']) 
- precode ( - Optional[- str]) –- Extra room for some code. This code will be evaluated within the loop of the sampling function (i.e. a value for lnM_1 is chosen already) - Examples: - precode = 'M_1=math.exp(lnM_1);' 
- postcode ( - Optional[- str]) – Code executed after the probability is calculated.
- probdist ( - str) –- Function determining the probability that gets assigned to the sampled parameter - Examples: - probdist = 'self.Kroupa2001(M_1)*M_1' 
- dphasevol ( - Union[- str,- int]) –- part of the parameter space that the total probability is calculated with. Put to -1 if you want to ignore any dphasevol calculations and set the value to 1 - Examples: - dphasevol = 'dlnM_1' 
- condition ( - Optional[- str]) –- condition that has to be met in order for the grid generation to continue - Examples: - condition = "self.population_options['binary']==1" 
- gridtype ( - str) – Method on how the value range is sampled. Can be either ‘edge’ (steps starting at the lower edge of the value range) or ‘centred’ (steps starting at- lower edge + 0.5 * stepsize).
- dry_parallel ( - Optional[- bool]) – If True, try to parallelize this variable in dry runs.
- topcode ( - Optional[- str]) – Code added at the very top of the block.
- bottomcode ( - Optional[- str]) – Code added at the very bottom of the block.
- dependency_variables ( - Optional[- list]) – TODO: describe
 
- Return type
- None
 
 - delete_grid_variable(name)[source]
- NOTE: this function exists for backward compatability with older scripts. Please use delete_sampling_variable from now on. - Function to delete a sampling variable with the given name. Calls delete_sampling_variable() - Parameters
- name ( - str) – name of the sampling variable to be deleted.
- Return type
- None
 
 - delete_sampling_variable(name)[source]
- Function to delete a sampling variable with the given name. - Parameters
- name ( - str) – name of the sampling variable to be deleted.
- Return type
- None
 
 - rename_grid_variable(oldname, newname)[source]
- NOTE: this function exists for backward compatability with older scripts. Please use rename_sampling_variable from now on. - Function to rename a sampling variable. Calls rename_sampling_variable() - note: this does NOT alter the order of the self.population_options[“_sampling_variables”] dictionary. - The order in which the sampling variables are loaded into the grid is based on their sampling_variable_number property - Parameters
- oldname ( - str) – old name of the sampling variable
- newname ( - str) – new name of the sampling variable
 
- Return type
- None
 
 - rename_sampling_variable(oldname, newname)[source]
- Function to rename a sampling variable. - note: this does NOT alter the order of the self.population_options[“_sampling_variables”] dictionary. - The order in which the sampling variables are loaded into the grid is based on their sampling_variable_number property - Parameters
- oldname ( - str) – old name of the sampling variable
- newname ( - str) – new name of the sampling variable
 
- Return type
- None
 
 - update_grid_variable(name, **kwargs)[source]
- NOTE: this function exists for backward compatability with older scripts. Please use update_sampling_variable from now on. - Function to update the values of a sampling variable. Calls update_sampling_variable(). - Parameters
- name ( - str) – name of the grid variable to be changed.
- **kwargs – key-value pairs to override the existing grid variable data. See add_sampling_variable for these names. 
 
- Return type
- None
 
 - update_sampling_variable(name, **kwargs)[source]
- Function to update the values of a sampling variable. - Parameters
- name ( - str) – name of the grid variable to be changed.
- **kwargs – key-value pairs to override the existing grid variable data. See add_sampling_variable for these names. 
 
- Return type
- None